﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Configuration;
using System.Web.UI;

using BizElements.Core;
using BizElements.BusinessLayer;
using BizElements.Web;

namespace MySolution.Web
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            SearchFilter.DefaultIntervalCriteria = SearchFilter.IntervalFlags.DefaultExceptText;
        }

        #region SessionStart, InitUserSession.

        protected void Session_Start(Object sender, EventArgs e)
        {
            UserSession.InitializeSession(CultureInfo.CurrentUICulture);
            UserSession.Actor = SuperUser.Instance;

            //if (AccountController.UsesWindowsAuthentication)
            //{
            //    AccountController.SignInWithWindowsAccount();
            //}
            //else if (AccountController.UsesFormsAuthentication)
            //{
            //    bool hasAuthTicket = (null != Request.Cookies[FormsAuthentication.FormsCookieName]);
            //    bool hasSessionExpired = hasAuthTicket && (UserSession.Actor == null);
            //    if (hasSessionExpired)
            //        AccountController.SignOut(false);
            //}
        }

        #endregion

        #region Error, RedirectToUserFriendlyErrorForm.

        protected void Application_Error(Object sender, EventArgs e)
        {
            RedirectToUserFriendlyErrorFormIfUiError();
        }

        private void RedirectToUserFriendlyErrorFormIfUiError()
        {
            // Redirect to user-friendly error form which doesn't reveal error details. Only possible if UI is used.
            bool isErrorInUI = HttpContext.Current.Handler is Page;
            if (isErrorInUI && ExceptionController.UseCustomErrorPage)
            {
                Exception error = Server.GetLastError();
                ExceptionController.RedirectToErrorForm(error);
            }
        }

        #endregion

        #region PreRequestHandlerExecute, TraceUserActivity.

        protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            TraceUserActivity();
        }

        private static void TraceUserActivity()
        {
            Page page = HttpContext.Current.CurrentHandler as Page;
            if (page != null && PageActivityTraceEnabled)
            {
                PageActivityTrace trace = new PageActivityTrace();
                trace.Actor = UserSession.Actor;
                trace.Filter = RequestFilter.Default;
                trace.Scope = PageTraceScope.Default;
                trace.StartTrace();
            }
        }

        private static bool PageActivityTraceEnabled
        {
            get
            {
                string cfgPageActivityTraceEnabled = ConfigurationManager.AppSettings["Global.PageActivityTraceEnabled"];
                return (cfgPageActivityTraceEnabled == "true") ? true : false;
            }
        }

        #endregion
    }
}
